package com.yksj.healthtalk.ui.doctorstation.active;

import org.universalimageloader.core.DefaultConfigurationFactory;
import org.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.TimeUtil;

/**
 * 
 * 中奖详细
 * @author origin
 *
 */
public class PrizeDetailActivity extends BaseFragmentActivity implements OnClickListener{
	
	JSONObject mJsonObject;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.prize_detail_layout);
		
		String content = null;
		if(arg0 != null){
			content = arg0.getString("json");
		}else{
			content = getIntent().getStringExtra("json");
		}
		try{
			mJsonObject = JSON.parseObject(content);
		}catch(JSONException e){
		}
		
		initUI();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("json", mJsonObject.toJSONString());
	}
	
	@Override
	protected void onActivityResult(int request, int result, Intent arg2) {
		super.onActivityResult(request, result, arg2);
		if(result == RESULT_OK){
			try{
				mJsonObject = JSON.parseObject(arg2.getStringExtra("json"));
				initUI();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	private void initUI(){
		initTitle();
		setTitle("奖品信息");
		setBackBtn("返回",this);
		
		
		findViewById(R.id.prize_detail).setOnClickListener(this);
		findViewById(R.id.address_btn).setOnClickListener(this);
		findViewById(R.id.address_txt).setOnClickListener(this);
		
		int state = mJsonObject.getIntValue("DELIVERY_STATUS");
		if(state < 20){
			findViewById(R.id.input_layout).setVisibility(View.VISIBLE);
			findViewById(R.id.address_layout).setVisibility(View.GONE);
		}else{
			findViewById(R.id.input_layout).setVisibility(View.GONE);
			findViewById(R.id.address_layout).setVisibility(View.VISIBLE);
		}
		
		TextView textView = (TextView)findViewById(R.id.time);
		textView.setText(TimeUtil.formatTime(mJsonObject.getString("GET_PRIZE_TIME")));
		textView = (TextView)findViewById(R.id.send_info);
		
		if(!TextUtils.isEmpty(mJsonObject.getString("DELIVERY_RESULT"))){
			textView.setText(mJsonObject.getString("DELIVERY_RESULT"));
		}
		ImageLoader.getInstance().displayImage(mJsonObject.getString("BIG_PIC_ADDR"),
				(ImageView)findViewById(R.id.prize_img),
				DefaultConfigurationFactory.createPrizeDraw(this));
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.address_txt:	
		case R.id.address_btn:
			Intent intent = new Intent(this,PostAddressActivity.class);
			intent.putExtra("json", mJsonObject.toJSONString());
			startActivityForResult(intent,200);
			break;
		case R.id.prize_detail://产品详情
			intent = new Intent(this,PrizeIntroduceActivity.class);
			intent.putExtra("json", mJsonObject.toJSONString());
			startActivityForResult(intent,100);
			break;
		case R.id.system_info:
			
			break;
		}
	}
}
